package com.zb.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.common.cache.CodeTableCache;
import com.zb.common.exception.BusinessException;
import com.zb.common.mapper.CodeTableMapper;
import com.zb.common.po.enums.PageSize;
import com.zb.common.po.model.CodeTable;
import com.zb.common.po.query.CodeTableQuery;
import com.zb.common.utils.PaginationResult;
import com.zb.common.utils.SimplePage;
import com.zb.service.CodeTableService;

@Service
public class CodeTableServiceImpl implements CodeTableService {

    @Resource
    private CodeTableMapper codeTableMapper;

    @Resource
    private CodeTableCache loclCodeTableCache;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = BusinessException.class)
    @Override
    public void addCodeTable(CodeTable codeTable) throws BusinessException {
        if (StringUtils.isEmpty(codeTable.getCode())
                || StringUtils.isEmpty(codeTable.getName())) {
            throw new BusinessException("参数错误");
        }
        if (codeTable.getPid() == null) {
            codeTable.setPid(0);
        }
        codeTable.setLastUpdateTime(new Date());
        codeTableMapper.insertCodeTable(codeTable);
        CodeTableQuery query = new CodeTableQuery();
        query.setPid(codeTable.getPid());
        query.setCode(codeTable.getCode());
        int count = codeTableMapper.selectCodeTableCount(query);
        if (count > 1) {
            throw new BusinessException("编码已经存在");
        }
        // 刷新缓存
        loclCodeTableCache.refreshCodeTableCache();
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = BusinessException.class)
    @Override
    public void updateCodeTable(CodeTable codeTable) throws BusinessException {
        if (codeTable.getId() == null
                || StringUtils.isEmpty(codeTable.getCode())
                || StringUtils.isEmpty(codeTable.getName())) {
            throw new BusinessException("参数错误");
        }
        if (codeTable.getPid() == null) {
            codeTable.setPid(0);
        }
        codeTable.setLastUpdateTime(new Date());
        codeTableMapper.updateCodeTable(codeTable);
        CodeTableQuery query = new CodeTableQuery();
        query.setPid(codeTable.getPid());
        query.setCode(codeTable.getCode());
        int count = codeTableMapper.selectCodeTableCount(query);
        if (count > 1) {
            throw new BusinessException("编码已经存在");
        }
        // 刷新缓存
        loclCodeTableCache.refreshCodeTableCache();
    }

    @Override
    public PaginationResult<CodeTable> findCodeTableList4Page(
            CodeTableQuery query) {
        int count = this.codeTableMapper.selectCodeTableCount(query);
        int pageSize = PageSize.SIZE20.getSize();
        int pageNo = 0;
        if (null != query.getPageNo()) {
            pageNo = query.getPageNo();
        }
        SimplePage page = new SimplePage(pageNo, count, pageSize);
        query.setPage(page);
        List<CodeTable> list = this.codeTableMapper.selectCodeTableList(query);
        PaginationResult<CodeTable> result = new PaginationResult<CodeTable>(
                page, list);
        return result;
    }

    @Override
    public CodeTable getCodeTable(int id) {
        CodeTableQuery query = new CodeTableQuery();
        query.setId(id);
        List<CodeTable> list = this.codeTableMapper.selectCodeTableList(query);
        if (!list.isEmpty() && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }
}
